package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.service.UploadFileService;
import io.minio.*;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import lombok.SneakyThrows;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

@Api(tags = "文件上传")
@RestController
@RequestMapping("/admin/product")
public class FileUploadController {

    @Autowired
    private UploadFileService uploadFileService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @SneakyThrows
    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile file){
        //  调用服务层方法: ctrl+alt+m
        String url = uploadFileService.fileUpload(file);
        //  返回数据--上传之后的文件路径
        return Result.ok(url);
    }

    public static void main(String[] args) {
        //        int length = args.length; .8; 8-11 jpg;
        String fileName = "atgu.igu.jpg";

//        System.out.println(fileName.substring(fileName.lastIndexOf(".")));
//        System.out.println(fileName.substring(8,12));
//        System.out.println(fileName.substring(9,12));
        System.out.println(FilenameUtils.getExtension(fileName));
        System.out.println(UUID.randomUUID().toString().replaceAll("-", ""));

    }

}
